/**
 *  RTTBuilder.java : IceBird project
 *  12:50:28 
 */
package icebird.compiler.ncomp;

import icebird.metadata.Class;

import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.StringWriter;
import java.util.ArrayList;

/**
 * Compiler for Runtime Type Table.
 * @author Sergey Shulepoff[Knott]
 */
public final class RTTBuilder {

	// Dont allow creation.
	private RTTBuilder() {
	}
	
	/**
	 * Compile Runtime Type Table.
	 * @return void
	 * @param c Classes to generate RTT.
	 */
	public static void compileRTT(ArrayList<Class> c) {
		StringBuffer buffer = new StringBuffer();
		declareRTT(buffer, c); // Declare RTT
		
		try {
			FileWriter wr = new FileWriter("obj/rtt.asm");
			wr.append(buffer);
			wr.close();
		} catch (Exception e) {
		}
	}
	
	private static void declareRTT(StringBuffer buffer, ArrayList<Class> c) {
		buffer.append("RTT:\n");
		
		emitDD(buffer, c.size());
		for (Class type : c) {
			emitRTIEntry(buffer, type);
		}
		
		buffer.append('\n');
	}
	
	private static void emitRTIEntry(StringBuffer buffer, Class c) {
		buffer.append("; " + c.getName() + "\n");
		emitDD(buffer, c.getTID()); // Emit RTID.
		emitVMT(buffer, c);
	}
	
	private static void emitVMT(StringBuffer buffer, Class c) {
		
	}
	
	private static void emitDD(StringBuffer buf, int value) {
		buf.append("dd " + value + "\n");
	}
}
